import requests
import  bs4
import csv

cookies = {
    'Hm_lvt_3e46284be460d08cb4b834b45a9b3bb6': '1612361894',
    'Hm_lvt_b9a324c2fdf4142db72952069584f3d0': '1612361894',
    'Hm_lpvt_b9a324c2fdf4142db72952069584f3d0': '1612362660',
    'Hm_lpvt_3e46284be460d08cb4b834b45a9b3bb6': '1612362660',
    'SERVERID': '33ddf3ccd93c8e477e529224884a682f|1612363673|1612363673',
}

headers = {
    'Host': 'www.hebaee.cn',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'X-Requested-With': 'XMLHttpRequest',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Origin': 'http://www.hebaee.cn',
    'Referer': 'http://www.hebaee.cn/search.html?&city=&assetsTypeParent=&assetsTypeLeaf=&state=%E5%B7%B2%E7%BB%93%E6%9D%9F&orderBy=&order=&keyWords=&minPrice=&maxPrice=&start=1',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
}

data = 'city=&assetsTypeParent=&assetsTypeLeaf=&state=%E5%B7%B2%E7%BB%93%E6%9D%9F&orderBy=&order=&start=1&keyWords=&minPrice=&maxPrice='

response = requests.post('http://www.hebaee.cn/projectlist/queryData', headers=headers, cookies=cookies, data=data)
totalPage = response.json()["page"]['totalPage']

headerName = ['detailsUrl', 'exchangeAssetsNo', 'city', 'announcementEndTime', 'county', 'projectType', 'orgId', 'lawsuitorName', 'province', 'assetsName', 'assetsTypeParent', 'assetstyPeRoot', 'from', 'id', 'state', 'announcementStartTime', 'approUnitName', 'startPrice', 'onSiteVisitApplicantsNumber', 'priceUnit', 'assetsTypeLeaf', 'priceType', 'childCount', 'projectNature', 'imgUrl', 'circuseeCount']

for page in range(1,totalPage+1):
    data = f"city=&assetsTypeParent=&assetsTypeLeaf=&state=%E5%B7%B2%E7%BB%93%E6%9D%9F&orderBy=&order=&start={page}&keyWords=&minPrice=&maxPrice="
    response = requests.post( 'http://www.hebaee.cn/projectlist/queryData',headers=headers,cookies=cookies,data=data )
    for each in response.json()[ "page" ][ "jsonArray" ][ "data" ]:
        print( each )
        with open("./index.csv",encoding="utf-8-sig",mode="a",errors="ignore",newline="") as f:
            writer = csv.DictWriter(f,fieldnames=headerName,extrasaction="ignore")
            writer.writerow(each)

